// pages/discounts/give/index.js
import api from "../../../api/index";
const { debounce } = require("../../../utils/util");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    coupon_id: null,
    searchParam: {},
    isUser: {},
    uLen: 0,
    //#region 列表
    list: [],
    isMore: true,
    page: 1,
    isLoading: false,
    //#endregion
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      coupon_id: options.id,
      list: [],
      isMore: true,
      page: 1,
      "searchParam.shop_id": wx.getStorageSync("shopID"),
    });
    this.getDataList();
  },
  getValue: debounce(function (e) {
    this.setData({
      "searchParam.search": e.detail.value,
      list: [],
      isMore: true,
      page: 1,
      show: false,
    });
    this.getDataList();
  }, 500),
  getDataList() {
    let { page, list, searchParam } = this.data;
    searchParam = {
      ...searchParam,
    };
    this.setData({
      isLoading: true,
    });
    api
      .apiStoreMemberList({
        ...searchParam,
        page,
      })
      .then((res) => {
        if (
          JSON.stringify(this.data.searchParam) !== JSON.stringify(searchParam)
        )
          return;
        let data = res.data;
        if (page >= data.all_page) this.setData({ isMore: false });
        if (data.list.length) {
          list.push(...data.list);
          this.setData({ list });
        }
      })
      .finally((_) => {
        this.setData({
          isLoading: false,
        });
      });
  },
  selectUser(e) {
    let isUser = this.data.isUser;
    let id = e.currentTarget.dataset.id;
    isUser[id] ? delete isUser[id] : (isUser[id] = true);
    this.setData({
      isUser,
      uLen: Object.keys(isUser).length,
    });
  },
  confirm() {
    let { isUser, uLen, coupon_id } = this.data;
    if (!uLen) {
      wx.showToast({
        title: "请选择要赠送的用户",
        icon: "none",
      });
      return;
    }
    wx.showModal({
      title: "确认赠送",
      content: `确认赠送优惠券？`,
      confirmText: "确认",
      cancelText: "取消",
      confirmColor: "#73F0EA",
      success: (res) => {
        if (res.confirm) {
          api
            .apiStoreCouponGive({
              coupon_id,
              user_id_list: Object.keys(isUser),
            })
            .then((res) => {
              if (res.code === 1) {
                wx.showToast({
                  title: "赠送成功",
                  icon: "none",
                });

                setTimeout((_) => {
                  const pages = getCurrentPages();
                  const prevPage = pages[pages.length - 2];
                  if (prevPage && prevPage.init) {
                    prevPage.init();
                  }
                  wx.navigateBack();
                }, 2500);
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: "none",
                });
              }
            });
        }
      },
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.isMore) return;
    if (this.data.isLoading) return;
    let page = this.data.page;
    this.setData({
      page: page + 1,
    });
    this.getDataList();
  },
  previewImage(e) {
    wx.previewImage({
      urls: e.currentTarget.dataset.url.split(";"),
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
